.( LOADING MATRIX MULTIPLICATION BENCHMARK...) CR
\ NOTE: THIS VERSION NEEDS 0.5MB DATA SPACE

\ A CLASSICAL BENCHMARK OF AN O(N**3) ALGORITHM; MATRIX MULTIPLICATION
\
\ PART OF THE PROGRAMS GATHERED BY JOHN HENNESSY FOR THE MIPS
\ RISC PROJECT AT STANFORD. TRANSLATED TO FORTH BY  MARTY FRAEMAN,
\ JOHNS HOPKINS UNIVERSITY/APPLIED PHYSICS LABORATORY.

\ MM FORTH2C DOESN'T HAVE IT !
: MYBOUNDS  OVER + SWAP ;

1 CELLS CONSTANT CELL

VARIABLE SEED

: INITIATE-SEED ( -- )  74755 SEED ! ;
: RANDOM  ( -- N )  SEED @ 1309 * 13849 + 65535 AND DUP SEED ! ;

200 CONSTANT ROW-SIZE
ROW-SIZE CELLS CONSTANT ROW-BYTE-SIZE

ROW-SIZE ROW-SIZE * CONSTANT MAT-SIZE
MAT-SIZE CELLS CONSTANT MAT-BYTE-SIZE

ALIGN CREATE IMA MAT-BYTE-SIZE ALLOT
ALIGN CREATE IMB MAT-BYTE-SIZE ALLOT
ALIGN CREATE IMR MAT-BYTE-SIZE ALLOT

: INITIATE-MATRIX ( M[ROW-SIZE][ROW-SIZE] -- )
  MAT-BYTE-SIZE MYBOUNDS DO
    RANDOM DUP 120 / 120 * - 60 - I !
  CELL +LOOP
;

: INNERPRODUCT ( A[ROW][*] B[*][COLUMN] -- INT)
  0 ROW-SIZE 0 DO
    >R OVER @ OVER @ * R> + >R
    SWAP CELL+ SWAP ROW-BYTE-SIZE +
    R>
  LOOP
  >R 2DROP R>
;

: MAIN  ( -- )
  INITIATE-SEED
  IMA INITIATE-MATRIX
  IMB INITIATE-MATRIX 
  IMR IMA MAT-BYTE-SIZE MYBOUNDS DO
    IMB ROW-BYTE-SIZE MYBOUNDS DO
      J I INNERPRODUCT OVER ! CELL+ 
    CELL +LOOP
  ROW-SIZE CELLS +LOOP
  DROP
;


MAIN
BYE
